
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { regS1istrationsAction } from './index';
import moment from 'moment';
import { Table } from 'antd';
import SearchList from './SeachList/SearchList';
import less from './RegS1.less';
import { axios } from 'utils';
import download from 'downloadjs';

class RegS1 extends Component{
    constructor(props) {
        super(props);
        this.state = {
            initTime: [moment().subtract('days', 7), moment().subtract('days', 1)],
        };
        this.columns = [
            { title: '时间', dataIndex: 'day', key: 'day'},
            { title: '注册人数', dataIndex: 'regNum', key: 'regNum'},
            { title: '借款1次(人数)', dataIndex: 'sub1', key: 'sub1'},
            { title: '借款2次(人数)', dataIndex: 'sub2', key: 'sub2'},
            { title: '借款3次(人数)', dataIndex: 'sub3', key: 'sub3'},
            { title: '借款4次(人数)', dataIndex: 'sub4', key: 'sub4'},
            { title: '借款4次以上(人数)', dataIndex: 'sub5', key: 'sub5'},
        ];
    }

    convertParams = (time) => {
        const isArr = Array.isArray(time) && time.length > 0;
        return {
            startTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00') : '',
            endTime: isArr ? time[1].format('YYYY-MM-DD 23:59:59') : ''
        };
    }
    handleSearch = (obj) => {
        const { time } = obj;
        const params = this.convertParams(time);
        const { getTableData } = this.props;
        getTableData({ ...params });
    }

    // handlReturn = (obj) => {
    //     const { time } = obj;
    //     const params = this.convertParams(time);
    //     console.log(params)
    //     axios({
    //         url: '/hs/admin/statistics/dayRegisterStatisticReport',
    //         method: 'post',
    //         responseType: 'blob',
    //         data: params
    //     }).then((res) => {
    //         download(res, `日转化率统计${Date.now()}.xls`);
    //     }).catch((err) => {
    //         console.log(err)
    //     });
    // }

    componentDidMount() {}

    componentWillUnmount() {
        const { setTableData } = this.props;
        setTableData([]);
    }

    render() {
        const { initTime } = this.state;
        const { tableData, loading } = this.props;
        return (
            <div>
                <SearchList handleSearch={this.handleSearch} initTime={initTime} loading={loading}/>
                <div className={less.tableTop}>
                    <Table columns={this.columns} dataSource={tableData} loading={loading} pagination={false} bordered scroll={{ x: '100%' }} />
                </div>
            </div>
        );

    }
}

const mapStateToProps = (state) => {
    const { businessStatisticsState: { regS1istrationsState } } = state;
    return {
        tableData: regS1istrationsState['tableData'],
        loading: regS1istrationsState['loading']
    };
}
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: regS1istrationsAction.ASSDGetTableData,
        setTableData: regS1istrationsAction.ASSDSetTableData
    }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(RegS1);